package com.example.healkep.MainActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.healkep.R;
import com.example.healkep.auth.HealthKitAuthActivity;
import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.hihealth.DataController;
import com.huawei.hms.hihealth.HiHealthStatusCodes;
import com.huawei.hms.hihealth.HuaweiHiHealth;
import com.huawei.hms.hihealth.data.DataType;
import com.huawei.hms.hihealth.data.Field;
import com.huawei.hms.hihealth.data.SamplePoint;
import com.huawei.hms.hihealth.data.SampleSet;

public class MainActivityShowSport extends Activity {
    private TextView textView1, textView2;
    private double cald=0;
    private int step=0, cali=0;
    private DataController dataController;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_show_sport);
        Button select1 = (Button) findViewById ( R.id.user );
        Button select2 = (Button) findViewById ( R.id.choose );
        textView1 = findViewById(R.id.cal);
        textView2 = findViewById(R.id.step);
        dataController = HuaweiHiHealth.getDataController(MainActivityShowSport.this);

        select2.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                // 给bnt1添加点击响应事件
                Intent intent2 = new Intent ( MainActivityShowSport.this, MainActivityShowSports.class );
                //启动
                startActivity ( intent2 );
            }
        } );
        // 1. 使用指定的数据类型（步数增量：DT_CONTINUOUS_STEPS_DELTA），调用readTodaySummation接口查询该数据类型的当天统计值
        Task<SampleSet> todaySummationTask = dataController.readTodaySummation(DataType.DT_CONTINUOUS_STEPS_DELTA);

// 2. 调用readTodaySummation接口查询当天的统计值是异步操作，需要设置成功或失败listener，返回数据查询成功或失败
// 注意：该接口查询的是某种数据类型的当天统计值，查询时间范围为当日00:00:00的时间戳至接口调用时当前系统时间戳。
// 该接口将开始时间，或者结束时间落在该查询时间范围内的所有数据点的数据值相加，并返回相加后的统计值
        todaySummationTask.addOnSuccessListener(new OnSuccessListener<SampleSet>() {
            @Override
            public void onSuccess(SampleSet sampleSet) {
                runOnUiThread ( new Runnable () {
                    @Override
                    public void run() {
                        Toast.makeText ( MainActivityShowSport.this, "获取成功", Toast.LENGTH_SHORT ).show ();
                    }
                } );
                if (sampleSet != null) {
                    showSampleSet(sampleSet);
                }
            }
        });
        todaySummationTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                String errorCode = e.getMessage();
                String errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode));
                System.out.println ( errorCode );
                if(Integer.parseInt (errorCode) == 50005) {
                    runOnUiThread ( new Runnable () {
                        @Override
                        public void run() {
                            Toast.makeText ( MainActivityShowSport.this, "请先点击账号管理进行授权后刷新", Toast.LENGTH_SHORT ).show ();
                        }
                    } );
                }
                else{
                    runOnUiThread ( new Runnable () {
                        @Override
                        public void run() {
                            Toast.makeText ( MainActivityShowSport.this, errorCode + ": " + errorMsg, Toast.LENGTH_SHORT ).show ();
                        }
                    } );
                }

            }
        });
        //绑定监听事件
        select1.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                // 给bnt1添加点击响应事件
                Intent intent1 = new Intent ( MainActivityShowSport.this, HealthKitAuthActivity.class );
                //启动
                startActivity ( intent1 );

            }
        } );
    }
    private void showSampleSet(SampleSet sampleSet) {

        for (SamplePoint samplePoint : sampleSet.getSamplePoints()) {
            for (Field field : samplePoint.getDataType().getFields()) {
                step =samplePoint.getFieldValue(field).asIntValue ();
                if(step>0)
                {
                    cald = step*0.02;
                    cali = (int)cald;
                    System.out.println (step);
                    System.out.println (cald);
                    System.out.println (cali);
                    textView1.setText(cali+"");
                    textView2.setText(step+"");
                }

            }
        }

    }
}
